<?php
class SwfUploadComponent extends Object
{
	    var $name = 'SwfUploadComponent';
	    var $params = array();
	    var $errorCode = null;
	    var $errorMessage = null;
	 
	    // file and path configuration
	    var $uploadpath;
	    var $webpath = '/files/';
	    var $overwrite = false;
	    var $filename;
	 
	    /**
050	     * Contructor function
051	     * @param Object &$controller pointer to calling controller
052	     */
	    function startup(&$controller) {
	        // initialize members
	        $this->uploadpath = 'files' . DS;
	 
	        //keep tabs on mr. controller's params
	        $this->params = $controller->params;
	 
	    }
	 
	    /**
063	     * Uploads a file to location
064	     * @return boolean true if upload was successful, false otherwise.
065	     */
	    function upload() {
	        $ok = false;
	        if ($this->validate()) {
	            $this->filename = $this->params['form']['Filedata']['name'];
	            $ok = $this->write();
	        }
	 
	        if (!$ok) {
	            header("HTTP/1.0 500 Internal Server Error");   //this should tell SWFUpload what's up
	            $this->setError();   //this should tell standard form what's up
	        }
	 
	        return ($ok);
	    }
	 
	    /**
082	     * finds a unique name for the file for the current directory
083	     * @param array an array of filenames which exist in the desired upload directory
084	     * @return string a unique filename for the file
085	     */
	    function findUniqueFilename($existing_files = null) {
	        // append a digit to the end of the name
	        $filenumber = 0;
	        $filesuffix = '';
	        $fileparts = explode('.', $this->filename);
	        $fileext = '.' . array_pop($fileparts);
	        $filebase = implode('.', $fileparts);
	 
	        if (is_array($existing_files)) {
	            do {
	                $newfile = $filebase . $filesuffix . $fileext;
	                $filenumber++;
	                $filesuffix = '(' . $filenumber . ')';
	            } while (in_array($newfile, $existing_files));
	        }
	 
	        return $newfile;
	    }
	 
	    /**
106	     * moves the file to the desired location from the temp directory
107	     * @return boolean true if the file was successfully moved from the temporary directory to the desired destination on the filesystem
108	     */
	    function write() {
	        // Include libraries
	        if (!class_exists('Folder')) {
	            uses ('folder');
	        }
	 
	        $moved = false;
	        $folder = new Folder($this->uploadpath, true, 0755);
	 
	        if (!$folder) {
	            $this->setError(1500, 'File system save failed.', 'Could not create requested directory: ' . $this->uploadpath);
        } else {
	            if (!$this->overwrite) {
	                $contents = $folder->ls();  //get directory contents
	                $this->filename = $this->findUniqueFilename($contents[1]);  //pass the file list as an array
	            }
	            if (!($moved = move_uploaded_file($this->params['form']['Filedata']['tmp_name'], $this->uploadpath . $this->filename))) {
	                $this->setError(1000, 'File system save failed.');
	            }
	        }
	        return $moved;
	    }
	 
	    /**
133	     * validates the post data and checks receipt of the upload
134	     * @return boolean true if post data is valid and file has been properly uploaded, false if not
135	     */
	    function validate() {
	        $post_ok = isset($this->params['form']['Filedata']);
	        $upload_error = $this->params['form']['Filedata']['error'];
	        $got_data = (is_uploaded_file($this->params['form']['Filedata']['tmp_name']));
	 
	        if (!$post_ok){
	            $this->setError(2000, 'Validation failed.', 'Expected file upload field to be named "Filedata."');
	        }
	        if ($upload_error){
	            $this->setError(2500, 'Validation failed.', $this->getUploadErrorMessage($upload_error));
	        }
	        return !$upload_error && $post_ok && $got_data;
	    }
	 
	    /**
151	     * parses file upload error code into human-readable phrase.
152	     * @param int $err PHP file upload error constant.
153	     * @return string human-readable phrase to explain issue.
154	     */
	    function getUploadErrorMessage($err) {
	        $msg = null;
	        switch ($err) {
	            case UPLOAD_ERR_OK:
	                break;
	            case UPLOAD_ERR_INI_SIZE:
	                $msg = ('The uploaded file exceeds the upload_max_filesize directive ('.ini_get('upload_max_filesize').') in php.ini.');
	                break;
	            case UPLOAD_ERR_FORM_SIZE:
	                $msg = ('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
                break;
	            case UPLOAD_ERR_PARTIAL:
	                $msg = ('The uploaded file was only partially uploaded.');
	                break;
	            case UPLOAD_ERR_NO_FILE:
	                $msg = ('No file was uploaded.');
	                break;
	            case UPLOAD_ERR_NO_TMP_DIR:
	                $msg = ('The remote server has no temporary folder for file uploads.');
	                break;
	            case UPLOAD_ERR_CANT_WRITE:
	                $msg = ('Failed to write file to disk.');
	                break;
	            default:
	                $msg = ('Unknown File Error. Check php.ini settings.');
	        }
	 
	        return $msg;
	    }
	 
	    /**
186	     * sets an error code which can be referenced if failure is detected by controller.
187	     * note: the amount of info stored in message depends on debug level.
188	     * @param int $code a unique error number for the message and debug info
189	     * @param string $message a simple message that you would show the user
190	     * @param string $debug any specific debug info to include when debug mode > 1
191	     * @return bool true unless an error occurs
192	     */
	    function setError($code = 1, $message = 'An unknown error occured.', $debug = '') {
	        $this->errorCode = $code;
	        $this->errorMessage = $message;
	        if (DEBUG) {
            $this->errorMessage .= $debug;
	        }
	        return true;
	    }
}
?>